package com.yanfan.schedule;


import com.yanfan.entity.IbmsRequestLogs;
import com.yanfan.entity.IbmsSystemUrl;
import com.yanfan.mapper.IbmsSystemUrlMapper;
import com.yanfan.service.IbmsRequestLogsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.List;

@Service
public class ScheduledApiMonitorService {

    @Autowired
    private IbmsSystemUrlMapper ibmsSystemUrlMapper;
    @Autowired
    private IbmsRequestLogsService ibmsRequestLogsService;

    @Autowired
    private RestTemplate restTemplate;

    // 每隔5秒执行一次健康检查
    //@Scheduled(fixedRate = 10000) //  5/s = 5000/ms
    public void checkApiStatuses() {
        // 从数据库中读取所有接口 URL
        List<IbmsSystemUrl> ibmsSystemUrls = ibmsSystemUrlMapper.selectList(null);
        if (ibmsSystemUrls.size() != 0) {
            for (IbmsSystemUrl ibmsSystemUrl : ibmsSystemUrls) {
                IbmsRequestLogs requestLogs = new IbmsRequestLogs();
                IbmsRequestLogs ibmsRequestLogs = ibmsRequestLogsService.selectLogInfoByUrl(ibmsSystemUrl.getUrl());

                try {
                    // 执行 HTTP 请求
                    restTemplate.getForObject(ibmsSystemUrl.getUrl(), String.class);
                    if (ObjectUtils.isEmpty(ibmsRequestLogs)) {
                        requestLogs.setUrl(ibmsSystemUrl.getUrl());
                        requestLogs.setStatus("SUCCESS");
                        requestLogs.setCreateTime(new Date());
                        ibmsRequestLogsService.save(requestLogs);
                    } else {
                        requestLogs.setId(ibmsRequestLogs.getId());
                        requestLogs.setStatus("SUCCESS");
                        requestLogs.setUpdateTime(new Date());
                        ibmsRequestLogsService.updateById(requestLogs);
                    }
                } catch (Exception e) {
                    if (ObjectUtils.isEmpty(ibmsRequestLogs)) {
                        requestLogs.setUrl(ibmsSystemUrl.getUrl());
                        requestLogs.setStatus("FAILURE");
                        requestLogs.setCreateTime(new Date());
                        ibmsRequestLogsService.save(requestLogs);
                    } else {
                        requestLogs.setId(ibmsRequestLogs.getId());
                        requestLogs.setStatus("FAILURE");
                        requestLogs.setUpdateTime(new Date());
                        ibmsRequestLogsService.updateById(requestLogs);
                    }
                } finally {
                    IbmsRequestLogs requestLog = ibmsRequestLogsService.selectLogInfoByUrl(ibmsSystemUrl.getUrl());
                    Long requestSum = requestLog.getRequestSum() + 1;
                    if (requestLog.getStatus().equals("SUCCESS")) {
                        Long requestSuccess = requestLog.getRequestSuccess() + 1;
                        requestLog.setRequestSum(requestSum);
                        requestLog.setRequestSuccess(requestSuccess);
                        double v = calculatePercentage(requestSuccess, requestSum);
                        requestLog.setSuccessZero(v);
                        requestLog.setFailZero(100.00-v);
                        requestLog.setStatus("SUCCESS");
                        requestLog.setUpdateTime(new Date());
                        ibmsRequestLogsService.updateById(requestLog);
                    }
                    if (requestLog.getStatus().equals("FAILURE")) {
                        Long requestFail = requestLog.getRequestFail() + 1;
                        requestLog.setRequestSum(requestSum);
                        requestLog.setRequestFail(requestFail);
                        double v = calculatePercentage(requestFail, requestSum);
                        requestLog.setFailZero(v);
                        requestLog.setSuccessZero(100-v);
                        requestLog.setStatus("FAILURE");
                        requestLog.setUpdateTime(new Date());
                        ibmsRequestLogsService.updateById(requestLog);
                    }
                }
            }
        }
    }


    // 计算百分比的方法
    public static double calculatePercentage(long requestFail, long requestSum) {
        if (requestSum == 0) {
            throw new IllegalArgumentException("requestSum 不能为零");
        }
        return (double) requestFail / requestSum * 100;
    }
}
